﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using MiddleMVCTask.BL.ViewModels;
using MiddleMvcTask.BL.Interfaces;
using MiddleMvcTask.Controllers;
using MiddleMvcTask.Data.DataModels;
using MiddleMvcTask.Data;
using System.Linq;
using MiddleMvcTask.AppStart;
using Moq;
using MiddleMVCTask.BL.Interfaces;
using MiddleMVCTask.BL;

namespace SimpleTestDemo
{
    [TestClass]
    public class UserBLTests
    {
        [TestMethod]
        public void SuccessfulLoginTest()
        {
            var hashPassword = Convert.ToBase64String(
                System.Security
                      .Cryptography
                      .SHA256
                      .Create()
                      .ComputeHash(System.Text.Encoding.UTF8.GetBytes("12345")));

            List<User> data = new List<User>()
            {
                new User()
                {
                    Username = "Name1",
                    Password = hashPassword
                }
            };

            var newUser = new LoginViewModel()
            {
                Password = "12345",
                Username = "Name1"
            };

            string cookie;

            var mockRepos = new Moq.Mock<IRepository<User>>();
            mockRepos.Setup(x => x.All()).Returns(data);

            var mockAuth = new Moq.Mock<IAuth>();
            mockAuth.Setup(x => x.DoAuth(It.IsAny<string>(), It.IsAny<bool>()))
                    .Callback(new Action<string, bool>((x,y) =>
                    {
                        cookie = x;
                    }));

            Casher.LoadStaticCache(mockRepos.Object);
           
            var userBL = new UserBL(mockRepos.Object,mockAuth.Object);

            userBL.LoginUser(newUser);

            Assert.AreEqual(true, userBL.Result.IsValid);
        }

        [TestMethod]
        public void WrongLoginTest()
        {
            var hashPassword = Convert.ToBase64String(
                System.Security
                      .Cryptography
                      .SHA256
                      .Create()
                      .ComputeHash(System.Text.Encoding.UTF8.GetBytes("12345")));

            List<User> data = new List<User>()
            {
                new User()
                {
                    Username = "Name1",
                    Password = hashPassword
                }
            };

            var newUser = new LoginViewModel()
            {
                Password = "1",
            };

            string cookie;

            var mockRepos = new Moq.Mock<IRepository<User>>();
            mockRepos.Setup(x => x.All()).Returns(data);

            var mockAuth = new Moq.Mock<IAuth>();
            mockAuth.Setup(x => x.DoAuth(It.IsAny<string>(), It.IsAny<bool>()))
                    .Callback(new Action<string, bool>((x,y) =>
                    {
                        cookie = x;
                    }));

            Casher.LoadStaticCache(mockRepos.Object);
           
            var userBL = new UserBL(mockRepos.Object,mockAuth.Object);

            userBL.LoginUser(newUser);

            Assert.AreEqual(false, userBL.Result.IsValid);
        }
    }
}